//
//  AppDelegate.m
//  UI 豆瓣完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    // 第一个视图控制器和导航控制器
    RootViewController *rootVC = [[RootViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
    // 设置TarBarItem
    UIImage *image1 = [UIImage imageNamed:@"activity.png"];
    
    nav.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"活动" image:image1 tag:1000] autorelease];
    // 第二组控制器
    SecondViewController *secVC = [[SecondViewController alloc] init];
    UINavigationController *secNAV = [[UINavigationController alloc] initWithRootViewController:secVC];
    UIImage *image2 = [UIImage imageNamed:@"movie.png"];
    
    secNAV.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"电影" image:image2 tag:1001] autorelease];
    // 第三组控制器
    ThirdViewController *thVC = [[ThirdViewController alloc] init];
    UINavigationController *thNAV = [[UINavigationController alloc] initWithRootViewController:thVC];
    UIImage *image3 = [UIImage imageNamed:@"cinema.png"];
    
    thNAV.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"影院" image:image3 tag:1002] autorelease];
    // 第四组控制器
    FourthViewController *fourVC = [[FourthViewController alloc] init];
    UINavigationController *fourNAV = [[UINavigationController alloc] initWithRootViewController:fourVC];
    UIImage *image4 = [UIImage imageNamed:@"user.png"];
    
    fourNAV.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"我的" image:image4 tag:1003] autorelease];
    // 创建标签控制器
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    // 把四组导航控制器放到一个数组中
    NSArray *viewControllers = [NSArray arrayWithObjects:nav, secNAV, thNAV, fourNAV, nil];
    // 给viewControllers属性赋值
    tabBarController.viewControllers = viewControllers;
   
    // tabBarcontroller 作为跟视图控制器
    _window.rootViewController = tabBarController;
    
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    
    nav.tabBarItem.badgeValue = @"666";
    secNAV.tabBarItem.badgeValue = @"666";
    thNAV.tabBarItem.badgeValue = @"666";
    
    tabBarController.delegate = self;
    
    [rootVC release];
    [nav release];
    [secVC release];
    [secNAV release];
    [thVC release];
    [thNAV release];
    [fourVC release];
    [fourNAV release];
    [tabBarController release];
    
    
    return YES;
}


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    viewController.tabBarItem.badgeValue = nil;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
